package com.eve.springcloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

/**
 * 全局过滤器 20200522
 * Gateway Filter：自定义过滤器
 * @author HXL
 *
 */
@Component
public class MyLogGatewayFilter implements GlobalFilter, Ordered {

	@Override
	public int getOrder() {
		// 数字越小优先级越高
		return 0;
	}

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		// 模拟：不被接受的访问用户
		String uname = exchange.getRequest().getQueryParams().getFirst("uname");
		// 全局检查，用户名是否正确
		if(uname == null) {
			System.out.println("不被接受的用户");
			// 不被接受的状态
			exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
			return exchange.getResponse().setComplete();
		}
		return chain.filter(exchange);
	}

}
